// 引入axios组件
import axios from 'axios';
import qs from 'qs'; // 处理数据  qs.stringify(params)
import { checkRule } from '@/assets/js/public';

export default {
  data() {
    return {
      title: '', // 面包屑标题
      search: '',
      modal1: false, // 模态框显示开关，true显示   false不显示
      param: '', // 点击编辑、删除的时候对应操作的那个id
      putpage: '', // 存储页数
      columns7: [
        {
          title: 'id',
          width: 100,
          key: 'id',
          render: (h, params) => h('div', [h('strong', params.row.id)]),
        },
        {
          title: '留言用户',
          key: 'nickname',
          render: (h, params) => h('div', [h('strong', params.row.nickname)]),
        },
        {
          title: '留言内容',
          key: 'content',
        },
        {
          title: '留言时间',
          key: 'addtime',
        },
        {
          title: '是否显示',
          key: 'action',
          align: 'center',
          render: (h, params) =>
            h('div', [
              h(
                'Button',
                {
                  props: {
                    type: 'success',
                    size: 'small',
                  },
                  style: {
                    marginRight: '5px',
                  },
                  on: {
                    click: () => {
                      this.is_show(params.row.id);
                    },
                  },
                },
                params.row.show,
              ),
            ]),
        },
        {
          title: '操作选项',
          key: 'action',
          align: 'center',
          render: (h, params) =>
            h('div', [
              /* h(
                "Button",
                {
                  props: {
                    type: "info",
                    size: "small"
                  },
                  style: {
                    marginRight: "5px"
                  },
                  on: {
                    click: () => {
                      this.show(params.row.id);
                    }
                  }
                },
                "编辑"
              ),// */
              h(
                'Button',
                {
                  props: {
                    type: 'error',
                    size: 'small',
                  },
                  style: {
                    marginRight: '5px',
                  },
                  on: {
                    click: () => {
                      this.remove(params);
                    },
                  },
                },
                '删除',
              ), //* /
            ]),
        },
      ],
    };
  },
  created() {
    const self = this;
    this.$Spin.show();
    setTimeout(() => {
      this.$Spin.hide();
    }, 500);
    const spage = this.$route.query.page;
    const asearch = this.$route.query.search;
    axios({
      method: 'post',
      url: '/admin/message/messageList',
      data: { page: spage, search: asearch },
    })
      .then((response) => {
        checkRule(response, self); // 检查是否有权限
        self.putpage = spage;
        self.$store.commit('getMessageInfo', response.data);
      })
      .catch((response) => {});
  },
  // 用来获取VUEX中的文件
  computed: {
    data() {
      // console.log(this.$store.state.product.productlistshow);
      return this.$store.state.message.messagelistshow;
    },
    page() {
      return this.$store.state.message.messagelist.total;
    },
  },
  methods: {
    // 切换是否显示
    is_show(messageid) {
      // 引入外部js文件中的方法
      // productaxios(shopid);
      // pro();
      const self = this;
      axios({
        method: 'post',
        url: '/admin/message/messageStatus',
        data: {
          messageid,
        },
      })
        .then((response) => {
          checkRule(response, self); // 检查是否有权限
          self.$store.commit('getMessageInfoAgain', self.putpage, self.search);
          self.$Message.success('操作成功');
        })
        .catch((response) => {});
    },

    // 分页的钩子函数，当跳页的时候重新请求（将页码发给后台）
    getpage(page) {
      if (page == undefined) {
        page = 1;
      }
      this.putpage = page;
      const self = this;
      axios({
        method: 'post',
        url: '/admin/message/messageList',
        data: {
          page,
          search: self.search,
        },
      })
        .then((response) => {
          self.$store.commit('getMessageInfo', response.data);
        })
        .catch((response) => {});
      this.$router.push({
        path: '/message', // 这是路由名称
        query: { page, search: self.search },
      });
    },

    /**
     * 删除当前商品
     * 同页面刷新不建议使用路由，再此请求数据
     * const定义的变量不可修改
     */
    remove(modelid) {
      this.modal1 = true;
      this.param = modelid;
    },
    ok() {
      const self = this; // 将this对象附到一个不可更改的变量中~
      axios({
        method: 'post',
        url: '/admin/message/messageDel',
        data: { messageid: self.param.row.id },
      })
        .then((response) => {
          checkRule(response, self); // 检查是否有权限
          if (response.data.code == 1) {
            const numm = response.data.count;
            const mo = numm % 10; // 判断剩余总数摸10是否是0
            const pagepage = Math.ceil(numm / 10);
            if (
              self.param.index == 0 &&
              self.putpage != 1 &&
              mo == 0 &&
              self.putpage != pagepage
            ) {
              // 判断当前所在页是否是最后一页。
              self.putpage -= 1;
            }
            self.$store.commit(
              'getMessageInfoAgain',
              self.putpage,
              self.search,
            );
            // console.log(self.putpage);
            self.$router.push({
              path: '/message', // 这是路由名称
              query: { page: self.putpage, search: self.search },
            });
            self.$Message.success(response.data.msg);
          } else {
            self.$Message.error(response.data.msg);
          }
        })
        .catch((response) => {});
    },
    cancel() {
      this.modal1 = false;
    },
  },
};
